<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>车辆管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">

<!-- 查询条件开始-->
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" method="post" id="searchFrm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">车牌号:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="carnumber"  autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">车辆类型:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="cartype"  autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">车辆颜色:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="color"  autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否出租:</label>
                    <div class="layui-input-inline">
                        <select name="isrenting">
                            <option value="">请选择状态</option>
                            <option value="1">已出租</option>
                            <option value="0">未出租</option>
                        </select>
                    </div>
                </div>
            </div>


            <div  class="layui-form-item" style="text-align: center;">
                <div class="layui-input-block" >
                    <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-submit lay-filter="doSearch">查询</button>
                    <button type="reset" class="layui-btn layui-btn-warm  layui-icon layui-icon-refresh">重置</button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="carTable" lay-filter="carTable"></table>
    <!-- 工具栏 -->
    <div id="carToolBar" style="display: none;">
        <button type="button" lay-event="add"  class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加车辆</button>
    </div>

    <div id="carRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger">
            <span class="layui-icon layui-icon-delete"></span>删除
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="addOrUpdateDiv" >
    <form class="layui-form layui-row layui-col-space10"  lay-filter="dataFrm" id="dataFrm">
        <div class="layui-col-md12 layui-col-xs12">
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md9 layui-col-xs7">
                    <div class="layui-form-item magt3">
                        <!--隐藏组件id                        -->
                        <input type="hidden" name="id">
                        <label class="layui-form-label">车牌号:</label>
                        <div class="layui-input-block">
                            <input type="text" name="carnumber" class="layui-input" lay-verify="required" placeholder="请输入车牌号">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆类型:</label>
                        <div class="layui-input-block">
                            <input type="text" name="cartype" class="layui-input" lay-verify="required" placeholder="请输入车辆类型">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆颜色:</label>
                        <div class="layui-input-block">
                            <input type="text" name="color" class="layui-input" lay-verify="required" placeholder="请输入车辆颜色">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3 layui-col-xs5">
                    <div class="layui-upload-list thumbBox mag0 magt3" id="carimgDiv">
                        <!-- 显示上传的图片 -->
                        <img class="layui-upload-img thumbImg" id="showCarImg" src="/file/showImages/images/defaultimg.jpg">
                        <!-- 保存当前显示图片的地址 -->
                        <input type="hidden" name="carimg" id="carimg">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-inline">
                    <label class="layui-form-label">购买价格:</label>
                    <div class="layui-input-block">
                        <input type="text" name="price" class="layui-input" lay-verify="required|number" placeholder="请输入购买价格">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">出租价格:</label>
                    <div class="layui-input-block">
                        <input type="text" name="rentprice" class="layui-input" lay-verify="required|number" placeholder="请输入出租价格">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">出租押金:</label>
                    <div class="layui-input-block">
                        <input type="text" name="deposit" class="layui-input" lay-verify="required|number" placeholder="请输入出租押金">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <label class="layui-form-label">车辆描述:</label>
                <div class="layui-input-block">
                    <input type="text" name="description" class="layui-input" lay-verify="required" placeholder="请输入车辆描述">
                </div>
            </div>
            <div class="layui-form-item magb0">
                <label class="layui-form-label">出租状态:</label>
                <div class="layui-input-block">
                    <input type="radio" name="isrenting" value="1" title="已出租">
                    <input type="radio" name="isrenting" value="0" title="未出租" checked="checked">
                </div>
            </div>
            <div class="layui-form-item magb0" style="text-align: center;">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
                </div>
            </div>
        </div>
    </form>

</div>
<!-- 添加和修改的弹出层结束 -->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer','upload'],function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var  upload=layui.upload;

        tableIns=table.render({
            elem: '#carTable'
            , url: '/bus/car/list'
            , toolbar: '#carToolBar' //开启头部工具栏，并为其绑定左侧模板
            , page: true
            ,height:"full-180"
            , title: '车辆数据表'
            , cols: [ [
                {type: 'checkbox'}
                , {field: 'id', title: '编号编号', align: 'center'}
                , {field: 'carnumber', title: '车牌号码', align: 'center'}
                , {field: 'cartype', title: '车辆类型', align: 'center'}
                , {field: 'color', title: '车辆颜色', align: 'center'}
                , {field: 'price', title: '购买价格', align: 'center'}
                , {field: 'rentprice', title: '出租价格', align: 'center'}
                , {field: 'deposit', title: '出租押金', align: 'center'}
                , {field: 'isrenting', title: '出租状态', align: 'center', templet: function (d) {
                        return d.isrenting == '1' ? '<font color=blue>已出租</font>' : '<font color=red>未出租</font>';
                    }
                }
                , {field: 'carimg', title: '缩略图', align: 'center',templet: function (d) {
                        return "<img src='/file/showImages/"+d.carimg+"'/>";
                    }
                }
                , {field: 'createtime', title: '录入时间', align: 'center'}
                , {fixed:'right',title: '操作', toolbar: '#carRowBar',width:200}
            ] ],
            done: function (res, curr, count) {
                //判断当前页面是否大于1并且当前页的数据为0
                if (curr > 1 && res.data.length == 0) {
                    var pageValue = curr - 1
                    //刷新数据表格
                    tableIns.reload({
                        page: {curr: pageValue}
                    });
                }
            }
        });

        //监听表单提交事件
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                url:'/bus/car/list',
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });

        //监听头部工具栏事件
        table.on('toolbar(carTable)',function (obj) {
            switch (obj.event) {
                case 'add':
                    //打开头部工具栏添加公告的窗口
                    openAddCar();
                    break;
            }
        });


        //监听行工具栏事件
        table.on('tool(carTable)',function (obj) {
            switch (obj.event) {
                case 'update':
                    //打开头部工具栏添加公告的窗口
                    openUpdateCar(obj.data);
                    break;
                case 'delete':
                    deleteCar(obj.data);
                    break
            }
        })





        var mainIndex;//打开窗口的索引
        var url;//提交请求的地址

        /**
         * 添加车辆
         */
        function openAddCar(){
            mainIndex= layer.open({
                type:1,
                content:$('#addOrUpdateDiv'),
                area:["800px","500px"],
                title:"添加车辆",
                success:function () {
                    //每次打开窗口，清空表单数据
                    $("#dataFrm")[0].reset();

                    //车辆的缩略图设置一个默认图片
                    $(".thumbImg").attr("src","/file/showImages/images/defaultimg.jpg");
                    $(".thumbBox").css("background","#fff");

                    //图片名称的隐藏域需要赋值
                    $("#carimg").val("");
                    //提交数据
                    url="/bus/car/addCar";
                }
            })
        }



        /**
         * 修改部门
         */
        function  openUpdateCar(data){
            mainIndex= layer.open({
                type:1,
                content:$('#addOrUpdateDiv'),
                area:["800px","500px"],
                title:"修改车辆",
                success:function () {
                    //表单数据回显
                    form.val("dataFrm",data);

                    //车辆的缩略图设置一个默认图片
                    $(".thumbImg").attr("src","/file/showImages/"+data.carimg);
                    //提交数据
                    url="/bus/car/updateCar";
                }

            })
        }



        /**
         * 删除
         */
        function deleteCar(data){
                    layer.confirm('是否要删除该数据？', {"icon":3,"title":"提示"},function (index) {
                        $.post("/bus/car/deleteById", {"id":data.id,"carimg":data.carimg}, function (result) {
                            if (result.success){
                                tableIns.reload();//表格重新加载
                            }
                            layer.msg(result.message);
                        }, "json");
                        layer.close(index);
                    });
        }


        //监听表单提交事件
        form.on("submit(doSubmit)",function (data) {
            $.post(url, data.field, function (result) {
                if (result.success){
                    //提示信息
                    layer.msg(result.message,{icon:1})

                    tableIns.reload();

                    layer.close(mainIndex);

                }else {
                    layer.msg(result.message,{icon:2});
                }
            }, "json");
            //禁止页面跳转
            return false;
        })


        //渲染upload组件
             upload.render({
            elem: '#carimgDiv' //绑定元素
            ,url: '/file/uploadFile' //上传接口
            ,method:'post'  //提交方式
            ,acceptMime:'image/*'//文件类型
            ,field:"attach"//以附件方式上传,名称必须与控制器方法中的参数名称保持一致
            ,done: function(res){
                //上传完毕回调
                 var path=res.imagePath;
                //图片上传成功后需要回显
                $(".thumbImg").attr("src","/file/showImages/"+path);
                $(".thumbBox").css("background","#fff");
                //图片名称的隐藏域需要赋值
                $("#carimg").val(path);
            }

        });

    });


</script>



</body>
</html>